package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.core.utility.IOUtils;
import com.jorado.core.utility.StringUtils;
import com.jorado.event.EventClient;
import com.jorado.json.JsonUtils;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import com.jorado.search.core.service.impl.FileExporter;
import org.springframework.context.annotation.Description;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Description("爬虫数据同步到文件")
public final class SpiderToFileService extends ExportToFileImpl<String> {

    private int total;

    public SpiderToFileService(String sourceFile, String distFile) {
        this(sourceFile, distFile, true);
    }

    public SpiderToFileService(String sourceFile, String distFile, boolean deleteIfExists) {
        super(new FileExporter(sourceFile, true), distFile, deleteIfExists);
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<String> dataList) {

        total += dataList.size();

        List<String> lines = new ArrayList<>();

        for (String line : dataList) {

            if (line.length() < 10) continue;

            DataInfo dataInfo = JsonUtils.fromJson(line.replace("\\n", " "), DataInfo.class);

            if (StringUtils.isNullOrWhiteSpace(dataInfo.getJobType())) continue;

            lines.add(String.format("%s……%s", dataInfo.getJobTitle(), dataInfo.getJobType()));
        }

        try {
            IOUtils.appendLines(this.distFile, lines);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取爬虫职位:[%d]个", total));

        return Result.OK;
    }

    /**
     * {"jobType":"销售代表\n客户代表","jobTitle":"区域经理"}
     */
    public static class DataInfo implements Serializable {

        private String jobType;
        private String jobTitle;

        public String getJobType() {
            return jobType;
        }

        public void setJobType(String jobType) {
            this.jobType = jobType;
        }

        public String getJobTitle() {
            return jobTitle;
        }

        public void setJobTitle(String jobTitle) {
            this.jobTitle = jobTitle;
        }
    }
}
